package com.painpointcloud.common.util;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class JackLeonArithUtil {
	private static final int DEF_DIV_SCALE=10; 
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		//System.out.println(stringToPercent("0.33"));
//		//System.out.println(stringToZeroDecimal("6.0"));
////		String str="34.2%";//字符串类型的百分数
////		double doubleValue= convertPercentToNumber(str);
//		System.out.println(isBetweenTwoDouble(new Double(6.8),new Double(2.6),new Double(9.8)));
//	}
	public static Long getStartPageIndex(Integer currentPage,Integer pageSize){
		return (long)pageSize*(currentPage-1);
	}
	/**
	 * 百分号的字符串转换为double型数字
	 * @param str 带百分号的字符串
	 * @return double型数字
	 */
	public static double convertPercentToNumber(String str){
		double doubleValue=0;
		if(str.contains("%")) {  
			str = str.replaceAll("%", "");  
            //Float f = Float.valueOf(str);  
			Double d=Double.valueOf(str);
            doubleValue=d/100;
        }  
		return doubleValue;
	}
	/**
	 * 根据传入的double字符串值返回精确到零位小数的字符串
	 * @param value
	 */
	public static String stringToZeroDecimal(String value){
		String percentValue="";
		if(StringUtils.isNotBlank(value)){
			try {
				 Double doubleValue=Double.parseDouble(value);
				 DecimalFormat df = new DecimalFormat("######0");  
		         percentValue= df.format(doubleValue);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}
		
         return percentValue;
	}
	/**
	 * 根据传入的double字符串值返回精确到两位小数的字符串
	 * @param value
	 */
	public static String stringToTwoDecimal(String value){
		String percentValue="";
		if(StringUtils.isNotBlank(value)){
			try {
				 Double doubleValue=Double.parseDouble(value);
				 DecimalFormat df = new DecimalFormat("######0.00");  
		         percentValue= df.format(doubleValue);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}
		
         return percentValue;
	}
	/**
	 * 根据传入的double字符串值返回百分比
	 * @param value
	 */
	public static String stringToPercent(String value){
		String percentValue="";
		if(StringUtils.isNotBlank(value)){
			try {
				 Double doubleValue=Double.parseDouble(value);
				 DecimalFormat df = new DecimalFormat("0%");  
		         percentValue= df.format(doubleValue);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}
		
         return percentValue;
	}
       
    public static double add(double d1,double d2){ 
        BigDecimal b1=new BigDecimal(Double.toString(d1)); 
        BigDecimal b2=new BigDecimal(Double.toString(d2)); 
        return b1.add(b2).doubleValue(); 
         
    } 
     
    public static double sub(double d1,double d2){ 
        BigDecimal b1=new BigDecimal(Double.toString(d1)); 
        BigDecimal b2=new BigDecimal(Double.toString(d2)); 
        return b1.subtract(b2).doubleValue(); 
         
    } 
     
    public static double mul(double d1,double d2){ 
        BigDecimal b1=new BigDecimal(Double.toString(d1)); 
        BigDecimal b2=new BigDecimal(Double.toString(d2)); 
        return b1.multiply(b2).doubleValue(); 
         
    } 
     
    public static double div(double d1,double d2){ 
 
        return div(d1,d2,DEF_DIV_SCALE); 
         
    } 
     
    public static double div(double d1,double d2,int scale){ 
        if(scale<0){ 
            throw new IllegalArgumentException("The scale must be a positive integer or zero"); 
        } 
        BigDecimal b1=new BigDecimal(Double.toString(d1)); 
        BigDecimal b2=new BigDecimal(Double.toString(d2)); 
        return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();    
    } 
    
    /**
     * 比较两个Double值是否相等
     * @param d1
     * @param d2
     * @return
     */
    public static boolean isEqual(double d1,double d2){ 
    	boolean bool=false;
        BigDecimal b1=new BigDecimal(Double.toString(d1)); 
        BigDecimal b2=new BigDecimal(Double.toString(d2)); 
        int result=b1.compareTo(b2);
        if(result==0){
        	bool=true;
        }
        return bool;    
    } 
    /**
     * 功能：判断value1是否在value2和value3之间 按照大于等于value2，小于value3判断
     * @param value1
     * @param value2
     * @param value3
     * @return
     */
    public static  boolean isBetweenTwoDouble(Double value1,Double value2,Double value3){
    	boolean bool=false;
    	bool=((value1>value2)||isEqual(value1,value2))&&(value1<value3) ? true : false;
    	return bool;
    }
    
    
//    public static void main(String[] args) {
//		System.out.println(isBetweenTwoDouble(new Double(0.9),new Double(1),new Double(2)));
//	}

}
